//
//  ChangeNameView.swift
//  ShouChong
//
//  Created by yw on 2024/8/14.
//

import SwiftUI

struct ChangeNameView: View {
    @AppStorage("user_name") var userName = ""
    @Environment(\.presentationMode) var mode
    var body: some View {
        ScrollView {
            HStack {
                Spacer()
                ShakeButton(label: 
                                Image(systemName: "xmark")
                    .foregroundColor(Color("color_gray"))
                            
                ) {
                    mode.wrappedValue.dismiss()
                }.modifier(MainTextModifier(fontWeight: .bold))
                    .padding(.horizontal,20)
                    .padding(.vertical,10)
                
            }
            .padding(.top,10)
            .padding(.bottom,20)
//            Spacer()
            
            TextField("请输入新的昵称", text: $userName)
                .padding(.horizontal,10)
                .frame(width: kScreenWidth - 20,height: 56)
                .background(Color("color_white"))
                .cornerRadius(10)
                
            HStack {
                
                Text("修改您的昵称")
                    .modifier(MainTextModifier(fontSize: 12, fontWeight: .regular, foreColor: Color("color_gray")))
                Spacer()
            }
//            .padding(.top,30)
            .padding(.horizontal,10)
            .padding(.bottom,30)
                
            ShakeButton(label: Text("确 定").frame(width: kScreenWidth - 50)) {
                changeUserName()
            }.modifier(MainButtonModifier(width: kScreenWidth - 50))
            
        }
//        .frame(height: kScreenHeight)
        .background(Color("color_bg"))
    }
    
    private func changeUserName() {
        let sendData = [
            "name":userName
        ]
        YWNetworkTool.share().requestJson(url: "info/editNNUserName", parameters: sendData) { res in
            DPrint(res)
            mode.wrappedValue.dismiss()
        } responseFailBlock: { error, code in
            YWToast.show(text: "修改失败")
        }

    }
}

